package com.crm.service.impl.contactManager;


import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.contactManager.ContactDetailMapper;
import com.crm.dao.mapper.contactManager.ContactInformationMapper;
import com.crm.dao.mapper.contactManager.ContactMapper;
import com.crm.dao.mapper.customMapper.contactManagerCustomMapper.ContactCustomMapper;
import com.crm.dao.mapper.customMapper.contactManagerCustomMapper.ContactInformationCustomMapper;
import com.crm.model.entity.contactManager.ContactDetail;
import com.crm.model.entity.contactManager.ContactDetailExample;
import com.crm.model.entity.contactManager.ContactInformation;
import com.crm.model.entity.contactManager.ContactInformationExample;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.contactManager.ContactInformationVO;
import com.crm.service.contactManager.ContactDetailDbService;
import com.crm.service.contactManager.ContactInformationDbService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 联系人管理信息
 */
@Service
public class ContactInformationDbServiceImpl implements ContactInformationDbService {
    @Autowired
    private ContactMapper contactMapper;

    @Autowired
    private ContactInformationMapper contactInformationMapper;

    @Autowired
    private ContactCustomMapper contactCustomMapper;

    @Autowired
    private ContactInformationCustomMapper contactInformationCustomMapper;

    /**
     * 新增联系方式
     * @param contactInformation 联系方式实体
     * @return
     */
    @Override
    public Boolean addContactInformation(ContactInformation contactInformation) {
        Boolean result = false;
        int num = contactInformationMapper.insertSelective(contactInformation);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 联系人-新增联系方式-联系方式重复
     * @param contactId  联系人id
     * @param contactInformation 联系方式
     * @return
     */
    @Override
    public ContactInformation getContactIdAndContactInformation(String contactId, String contactInformation) {
        ContactInformationExample contactInformationExample = new ContactInformationExample();
        ContactInformationExample.Criteria criteria = contactInformationExample.createCriteria();
        criteria.andContactIdEqualTo(contactId);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactInformation> list = contactInformationMapper.selectByExample(contactInformationExample);
        if(list.size() == 0){
            return new ContactInformation();
        }else{
            return list.get(0);
        }

    }

    /**
     * 联系人-修改联系方式
     * @param contactInformation 联系方式实体
     * @return
     */
    @Override
    public Boolean updateContactInformation(ContactInformation contactInformation) {
        Boolean result = false;
        int num = contactInformationMapper.updateByPrimaryKeySelective(contactInformation);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 查询是否是主联系人
     * @param contactId  联系人id
     * @param contactInformationType 联系方式类型
     * @return
     */
    @Override
    public List<ContactInformation> getContactIdAndContactInformationType(String contactId, int contactInformationType) {
        ContactInformationExample contactInformationExample = new ContactInformationExample();
        ContactInformationExample.Criteria criteria = contactInformationExample.createCriteria();
        criteria.andContactIdEqualTo(contactId);
        criteria.andContactInformationTypeEqualTo(contactInformationType);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactInformation> list = contactInformationMapper.selectByExample(contactInformationExample);
        return list;
    }

    /**
     * 根据联系人id查询联系人下的联系方式
     * @param order 分组
     * @param contactId 联系人id
     * @return
     */
    @Override
    public List<ContactInformation> contactInformationList(String order, String contactId) {
        ContactInformationExample contactInformationExample = new ContactInformationExample();
        contactInformationExample.setOrderByClause(order);
        ContactInformationExample.Criteria criteria = contactInformationExample.createCriteria();
        criteria.andContactIdEqualTo(contactId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactInformation> list = contactInformationMapper.selectByExample(contactInformationExample);
        return list;

    }

    /**
     * 修改联系方式废止状态
     * @param contactInformation 联系人实体
     * @return
     */
    @Override
    public Boolean updateAbolishStateById(ContactInformation contactInformation) {
        return contactInformationMapper.updateByPrimaryKeySelective(contactInformation) > 0;
    }

    /**
     * 联系人-新增联系人--同一客户下联系方式不能一样
     * @param customerId 客户id
     * @param contactInformation 联系方式
     * @param contactNature  联系人性质  0企业联系人  1非企业联系人
     * @return
     */
    @Override
    public List<ContactInformationVO> getCustomerIdAndContactInformation(String customerId, String contactInformation, String contactNature) {
        /*ContactInformationExample contactInformationExample = new ContactInformationExample();
        ContactInformationExample.Criteria criteria = contactInformationExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactInformation> list = contactInformationMapper.selectByExample(contactInformationExample);
        return list;*/
        return contactInformationCustomMapper.getCustomerIdAndContactInformation(customerId,contactInformation);
    }

    /**
     * 逻辑删除  联系人下的联系方式
     * @param contactInformation 联系方式实体类
     * @return
     */
    @Override
    public Boolean deleteContactInformationById(ContactInformation contactInformation) {
        return contactInformationMapper.updateByPrimaryKeySelective(contactInformation) > 0;
    }


    /***
     * @Description:根据联系人id和公司id查询联系人信息
     * @Param: contactId联系人id
     * @Param: companyId公司id
     * @Author: luojie
     * @Date: 2021/6/28 14:52
     */
    @Override
    public List<ContactInformation> selectContactInformationByContactIdAndCompanyId(String contactId,String accountId,String companyId) {
        ContactInformationExample contactInformationExample = new ContactInformationExample();
        ContactInformationExample.Criteria criteria = contactInformationExample.createCriteria();
        criteria.andContactIdEqualTo(contactId);
        criteria.andCreatePeopleIdEqualTo(accountId);
        criteria.andCompanyIdEqualTo(companyId);
        //20210825新加的   废止的联系方式不能展示
        criteria.andAbolishStateEqualTo(1);
        List<ContactInformation> contactInformations = contactInformationMapper.selectByExample(contactInformationExample);
        return contactInformations;
    }

    /**
     * 通过联系人id修改联系方式表的删除状态
     * @param contactInformation 联系方式实体类
     * @return
     */
    @Override
    public Boolean updateInformation(ContactInformation contactInformation) {
        Boolean result = false;
        int num = contactCustomMapper.updateInformation(contactInformation);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 呼叫中心根据手机号和账号Id查询联系人
     * @Author pym
     * @Date  2021/7/1 18:15
     * @param contactInformation 联系方式
     * @param accountId 账号Id
     * @return {@link {com.crm.model.vo.contactManager.ContactInformationVO}}
     */
    @Override
    public ContactInformationVO getContactInformationByContactInfoAndId(String contactInformation, String accountId) {
        List<ContactInformationVO> contactInformationList = contactInformationCustomMapper.selectContactInformationList(contactInformation, accountId);
        if(contactInformationList != null && contactInformationList.size() > 0){
            return contactInformationList.get(0);
        }
        return null;
    }

    /**
     * 客户导入判断联系方式是否存在
     * @Author pym
     * @Date  2021/7/2 14:06
     * @param contactId 联系人Id
     * @param contactInformation 联系方式
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean isContactInformationRepeat(String contactId, String contactInformation) {
        Boolean result = false;
        ContactInformationExample contactInformationExample = new ContactInformationExample();
        ContactInformationExample.Criteria criteria = contactInformationExample.createCriteria();
        criteria.andContactIdEqualTo(contactId);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactInformation> list = contactInformationMapper.selectByExample(contactInformationExample);
        if(list.size() > 0){
            result = true;
        }
        return result;
    }

}
